package com.corona.game.util.texture;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.corona.game.util.SmolSprite;
import com.corona.game.util.SmolSpriteProxy;
import com.corona.game.util.SpriteProxy;
import com.corona.game.util.TextureCache;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/* loaded from: pokewilds.jar:com/pkmngen/game/util/texture/DynamicTextures.class */
public class DynamicTextures {
    private static final String PREFIX = "";
    public static final int SHORELINES_COUNT = 0;
    public static final int WATER_HOLE_COUNT = 0;
    public static final int CHIMNEY_COUNT = 0;
    private static final Set<String> addedTiles = new HashSet();
    private static Map<String, TextureAtlas.AtlasRegion> texturesCache = new HashMap();

    public static TextureAtlas.AtlasRegion get(String name) {
        String key = cacheKey(name);
        if (!texturesCache.containsKey(key)) {
            texturesCache.put(key, TilesAtlas.get(PREFIX, name));
        }
        return texturesCache.get(key);
    }

    public static TextureRegion get(String name, int index) {
        String key = cacheKey(name, index);
        if (!texturesCache.containsKey(key)) {
            texturesCache.put(key, TilesAtlas.get(PREFIX, name, index));
        }
        return texturesCache.get(key);
    }

    private static void addToLocalCache(String name) {
        texturesCache.put(cacheKey(name), TilesAtlas.get(PREFIX, name));
    }

    private static void addToLocalCache(String name, int index) {
        texturesCache.put(cacheKey(name, index), TilesAtlas.get(PREFIX, name, index));
    }

    private static String cacheKey(String name) {
        return cacheKey(name, -1);
    }

    private static String cacheKey(String name, int index) {
        return name + "_" + index;
    }

    private static Rectangle pack(String name, Pixmap pixmap) {
        return TilesAtlas.pack(PREFIX, name, pixmap);
    }

    private static Rectangle pack(String name, int index, Pixmap pixmap) {
        return TilesAtlas.pack(PREFIX, name, index, pixmap);
    }

    private static void updateTextureAtlas() {
        TilesAtlas.update();
    }

    public static void cacheShorelines(String shoreType) {
        Pixmap currPixmap;
        int scale;
        if (!addedTiles.contains(shoreType)) {
            TextureData temp = TextureCache.get(Gdx.files.internal("tiles/shore_sheet4.png")).getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap outerPixmap = temp.consumePixmap();
            TextureData temp2 = TextureCache.get(Gdx.files.internal("tiles/shore1inner_sheet2.png")).getTextureData();
            if (!temp2.isPrepared()) {
                temp2.prepare();
            }
            Pixmap innerPixmap = temp2.consumePixmap();
            Vector2 quadNE = new Vector2(24.0f, 24.0f);
            if (shoreType.contains("[E]")) {
                quadNE.add(16.0f, 0.0f);
            }
            if (shoreType.contains("[N]")) {
                quadNE.add(0.0f, 16.0f);
            }
            if (shoreType.contains("[NE]") && !shoreType.contains("[N]") && !shoreType.contains("[E]")) {
                quadNE = null;
            }
            Vector2 quadNW = new Vector2(16.0f, 24.0f);
            if (shoreType.contains("[W]")) {
                quadNW.add(-16.0f, 0.0f);
            }
            if (shoreType.contains("[N]")) {
                quadNW.add(0.0f, 16.0f);
            }
            if (shoreType.contains("[NW]") && !shoreType.contains("[N]") && !shoreType.contains("[W]")) {
                quadNW = null;
            }
            Vector2 quadSE = new Vector2(24.0f, 16.0f);
            if (shoreType.contains("[E]")) {
                quadSE.add(16.0f, 0.0f);
            }
            if (shoreType.contains("[S]")) {
                quadSE.add(0.0f, -16.0f);
            }
            if (shoreType.contains("[SE]") && !shoreType.contains("[S]") && !shoreType.contains("[E]")) {
                quadSE = null;
            }
            Vector2 quadSW = new Vector2(16.0f, 16.0f);
            if (shoreType.contains("[W]")) {
                quadSW.add(-16.0f, 0.0f);
            }
            if (shoreType.contains("[S]")) {
                quadSW.add(0.0f, -16.0f);
            }
            if (shoreType.contains("[SW]") && !shoreType.contains("[S]") && !shoreType.contains("[W]")) {
                quadSW = null;
            }
            for (int k = 0; k < 7; k++) {
                Pixmap newPixmap = new Pixmap(16, 16, Pixmap.Format.RGBA8888);
                Vector2[] offsets = {new Vector2(0.0f, 0.0f), new Vector2(24.0f, 0.0f), new Vector2(0.0f, 24.0f), new Vector2(24.0f, 24.0f)};
                Vector2[] origins = {new Vector2(0.0f, 0.0f), new Vector2(8.0f, 0.0f), new Vector2(0.0f, 8.0f), new Vector2(8.0f, 8.0f)};
                Vector2 offset = new Vector2();
                Vector2[] quads = {quadNW, quadNE, quadSW, quadSE};
                int i = 0;
                while (i < quads.length && i < offsets.length) {
                    if (quads[i] == null) {
                        offset.set(offsets[i]);
                        currPixmap = innerPixmap;
                        scale = 32;
                    } else {
                        offset.x = quads[i].x;
                        offset.y = 40.0f - quads[i].y;
                        currPixmap = outerPixmap;
                        scale = 48;
                    }
                    newPixmap.drawPixmap(currPixmap, (int) origins[i].x, (int) origins[i].y, ((int) offset.x) + (k * scale), (int) offset.y, 8, 8);
                    i++;
                }
                pack(shoreType, k, newPixmap);
            }
            updateTextureAtlas();
            for (int k2 = 0; k2 < 7; k2++) {
                addToLocalCache(shoreType, k2);
            }
            addedTiles.add(shoreType);
        }
    }

    public static void cachePuddle(String tileName, String baseTile, String directions) {
        int offsetY;
        int offsetX;
        if (!addedTiles.contains(tileName)) {
            Texture text = TextureCache.get(Gdx.files.internal("tiles/puddle_sheet1.png"));
            SpriteProxy tempSprite = new SpriteProxy(text, 0, 0, text.getWidth(), text.getHeight());
            TextureData temp = text.getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap currPixmap = temp.consumePixmap();
            Vector2 quadNE = new Vector2(40.0f, 8.0f);
            if (directions.contains("[E]")) {
                quadNE.set(8.0f, 8.0f);
            }
            if (directions.contains("[N]")) {
                quadNE.set(24.0f, 8.0f);
            }
            if (directions.contains("[N]") && directions.contains("[E]")) {
                quadNE.set(56.0f, 8.0f);
            }
            Vector2 quadNW = new Vector2(32.0f, 8.0f);
            if (directions.contains("[W]")) {
                quadNW.set(0.0f, 8.0f);
            }
            if (directions.contains("[N]")) {
                quadNW.set(16.0f, 8.0f);
            }
            if (directions.contains("[N]") && directions.contains("[W]")) {
                quadNW.set(48.0f, 8.0f);
            }
            Vector2 quadSE = new Vector2(40.0f, 0.0f);
            if (directions.contains("[E]")) {
                quadSE.set(8.0f, 0.0f);
            }
            if (directions.contains("[S]")) {
                quadSE.set(24.0f, 0.0f);
            }
            if (directions.contains("[S]") && directions.contains("[E]")) {
                quadSE.set(56.0f, 0.0f);
            }
            Vector2 quadSW = new Vector2(32.0f, 0.0f);
            if (directions.contains("[W]")) {
                quadSW.set(0.0f, 0.0f);
            }
            if (directions.contains("[S]")) {
                quadSW.set(16.0f, 0.0f);
            }
            if (directions.contains("[S]") && directions.contains("[W]")) {
                quadSW.set(48.0f, 0.0f);
            }
            Texture text2 = TextureCache.get(Gdx.files.internal("tiles/" + baseTile + ".png"));
            TextureData temp2 = text2.getTextureData();
            if (!temp2.isPrepared()) {
                temp2.prepare();
            }
            Pixmap basePixmap = temp2.consumePixmap();
            Pixmap newPixmap = new Pixmap(basePixmap.getWidth(), basePixmap.getHeight(), Pixmap.Format.RGBA8888);
            int height = text2.getHeight();
            int width = text2.getWidth();
            int i = 0;
            int j = 0;
            while (j < height) {
                if (i > width) {
                    i = -1;
                    j++;
                } else {
                    if (i < 8) {
                        if (j < 8) {
                            offsetX = (int) quadNW.x;
                            offsetY = 8 - ((int) quadNW.y);
                        } else {
                            offsetX = (int) quadSW.x;
                            offsetY = 8 - ((int) quadSW.y);
                        }
                    } else if (j < 8) {
                        offsetX = (int) quadNE.x;
                        offsetY = 8 - ((int) quadNE.y);
                    } else {
                        offsetX = (int) quadSE.x;
                        offsetY = 8 - ((int) quadSE.y);
                    }
                    int color2 = currPixmap.getPixel((i % 8) + offsetX, (j % 8) + offsetY);
                    if (color2 == 0 || color2 == 943208703 || color2 == 1891672064 || color2 == -1869023232) {
                        color2 = basePixmap.getPixel(i, j);
                    } else if (color2 == -1684300801) {
                        color2 = Color.rgba8888(tempSprite.color1);
                    } else if (color2 == -1869022977) {
                        Color tempColor = new Color(basePixmap.getPixel(i, j));
                        tempColor.a = 0.25f;
                        color2 = Color.rgba8888(tempColor);
                    }
                    newPixmap.drawPixel(i, j, color2);
                }
                i++;
            }
            pack(tileName, newPixmap);
            updateTextureAtlas();
            addToLocalCache(tileName);
            addedTiles.add(tileName);
        }
    }

    public static void cacheColoredHouseBed(String name) {
        if (!addedTiles.contains(name)) {
            String carpetName = name.replace("house_bed1colored", "");
            TextureData temp = TextureCache.get(Gdx.files.internal("tiles/buildings/house_bed1_colorbase.png")).getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap bedPixmap = temp.consumePixmap();
            TextureData temp2 = TextureCache.get(Gdx.files.internal("tiles/buildings/" + carpetName + ".png")).getTextureData();
            if (!temp2.isPrepared()) {
                temp2.prepare();
            }
            Pixmap carpetPixmap = temp2.consumePixmap();
            Pixmap newPixmap = new Pixmap(16, 32, Pixmap.Format.RGBA8888);
            newPixmap.setColor(0.0f, 0.0f, 0.0f, 0.0f);
            newPixmap.drawPixmap(carpetPixmap, 0, 12);
            newPixmap.drawPixmap(bedPixmap, 0, 0);
            pack(name, newPixmap);
            updateTextureAtlas();
            addToLocalCache(name);
            addedTiles.add(name);
        }
    }

    public static void cacheWaterHole(String name) {
        if (!addedTiles.contains(name)) {
            String[] names = name.split("_");
            String ending = "";
            if (names.length > 2 && !names[names.length - 1].equals("default")) {
                ending = "_" + names[names.length - 1];
            }
            for (int k = 0; k < 4; k++) {
                Texture text = TextureCache.get(Gdx.files.internal("tiles/hole1" + ending + ".png"));
                TextureData temp = text.getTextureData();
                if (!temp.isPrepared()) {
                    temp.prepare();
                }
                Pixmap newPixmap = temp.consumePixmap();
                int height = text.getHeight();
                int width = text.getWidth();
                TextureData temp2 = TextureCache.get(Gdx.files.internal("tiles/water2.png")).getTextureData();
                if (!temp2.isPrepared()) {
                    temp2.prepare();
                }
                Pixmap currPixmap = temp2.consumePixmap();
                int i = 0;
                int j = 0;
                while (j < height) {
                    if (i > width) {
                        i = -1;
                        j++;
                    } else {
                        Color color3 = new Color(newPixmap.getPixel(i, j));
                        boolean z = color3.r == 0.21960784f && color3.g == 0.21960784f && color3.b == 0.21960784f;
                        if (color3.a != 0.0f) {
                            Color color2 = new Color(currPixmap.getPixel(i + k, j));
                            newPixmap.drawPixel(i, j, Color.rgba8888(color2.r, color2.g, color2.b, color2.a));
                        }
                    }
                    i++;
                }
                pack(name, k, newPixmap);
            }
            updateTextureAtlas();
            for (int k2 = 0; k2 < 4; k2++) {
                addToLocalCache(name, k2);
            }
            addedTiles.add(name);
        }
    }

    public static void cacheHole(String name) {
        if (!addedTiles.contains(name)) {
            Texture text = TextureCache.get(Gdx.files.internal("tiles/" + name.replace("[NE]", "").replace("[SE]", "").replace("[SW]", "").replace("[NW]", "") + ".png"));
            TextureData temp = text.getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap newPixmap = temp.consumePixmap();
            int height = text.getHeight();
            int width = text.getWidth();
            int i = 0;
            int j = 0;
            while (j < height) {
                if (i > width) {
                    i = -1;
                    j++;
                } else if (j < 10) {
                    if (i < 8) {
                        if (name.contains("[NW]")) {
                            newPixmap.drawPixel(i, j, Color.rgba8888(0.627451f, 0.47058824f, 0.09411765f, 1.0f));
                        }
                    } else if (name.contains("[NE]")) {
                        newPixmap.drawPixel(i, j, Color.rgba8888(0.627451f, 0.47058824f, 0.09411765f, 1.0f));
                    }
                } else if (i < 8) {
                    if (name.contains("[SW]")) {
                        newPixmap.drawPixel(i, j, Color.rgba8888(0.627451f, 0.47058824f, 0.09411765f, 1.0f));
                    }
                } else if (name.contains("[SE]")) {
                    newPixmap.drawPixel(i, j, Color.rgba8888(0.627451f, 0.47058824f, 0.09411765f, 1.0f));
                }
                i++;
            }
            pack(name, newPixmap);
            updateTextureAtlas();
            addedTiles.add(name);
        }
    }

    public static void cacheInteriorWall(String tileName) {
        int offsetX;
        int offsetY;
        if (!addedTiles.contains(tileName)) {
            TextureData temp = TextureCache.get(Gdx.files.internal("tiles/buildings/interiorwall1.png")).getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap currPixmap = temp.consumePixmap();
            Pixmap newPixmap = new Pixmap(16, 32, Pixmap.Format.RGBA8888);
            Vector2 bl = new Vector2(0.0f, 0.0f);
            Vector2 tr = new Vector2(8.0f, 8.0f);
            if (tileName.contains("N")) {
                bl.add(0.0f, 8.0f);
            }
            if (tileName.contains("S")) {
                tr.add(0.0f, -8.0f);
            }
            if (tileName.contains("E")) {
                tr.add(-8.0f, 0.0f);
            }
            if (tileName.contains("W")) {
                bl.add(8.0f, 0.0f);
            }
            int i = 0;
            int j = 0;
            while (j < 16) {
                if (i > 16) {
                    i = -1;
                    j++;
                } else {
                    if (i < 8) {
                        offsetX = (int) bl.x;
                    } else {
                        offsetX = (int) tr.x;
                    }
                    if (j < 8) {
                        offsetY = (int) bl.y;
                    } else {
                        offsetY = (int) tr.y;
                    }
                    newPixmap.drawPixel(i, j, currPixmap.getPixel(i + offsetX, j + offsetY));
                }
                i++;
            }
            TextureData temp2 = TextureCache.get(Gdx.files.internal("tiles/buildings/house5_wall1.png")).getTextureData();
            if (!temp2.isPrepared()) {
                temp2.prepare();
            }
            Pixmap currPixmap2 = temp2.consumePixmap();
            int i2 = 0;
            int j2 = 0;
            while (j2 < 16) {
                if (i2 > 16) {
                    i2 = -1;
                    j2++;
                } else {
                    newPixmap.drawPixel(i2, j2 + 16, currPixmap2.getPixel(i2, j2));
                }
                i2++;
            }
            String[] names = tileName.split("\\|");
            if (names.length > 1) {
                TextureData temp3 = TextureCache.get(Gdx.files.internal("tiles/buildings/" + names[1] + ".png")).getTextureData();
                if (!temp3.isPrepared()) {
                    temp3.prepare();
                }
                Pixmap currPixmap3 = temp3.consumePixmap();
                int i3 = 0;
                int j3 = 0;
                while (j3 < 16) {
                    if (i3 > 16) {
                        i3 = -1;
                        j3++;
                    } else {
                        newPixmap.drawPixel(i3, j3 + 16, currPixmap3.getPixel(i3, j3));
                    }
                    i3++;
                }
            }
            pack(tileName, newPixmap);
            updateTextureAtlas();
            addToLocalCache(tileName);
            addedTiles.add(tileName);
        }
    }

    public static void cacheHouseWithWindow(String name, SmolSprite overSprite) {
        int color2;
        if (!addedTiles.contains(name)) {
            Pixmap oldPixmap = overSprite.getPixmap();
            int left = overSprite.getRegionX();
            int bottom = overSprite.getRegionY();
            Pixmap newPixmap = new Pixmap(overSprite.getRegionWidth(), overSprite.getRegionHeight(), Pixmap.Format.RGBA8888);
            TextureData temp = TextureCache.get(Gdx.files.internal("tiles/buildings/exteriorwindows1.png")).getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap currPixmap = temp.consumePixmap();
            boolean touchEast = name.contains("E");
            boolean touchWest = name.contains("W");
            if (name.contains("house5")) {
                touchEast |= name.contains("left1");
                touchWest |= name.contains("right1");
            }
            for (int m = 0; m < newPixmap.getWidth(); m++) {
                for (int n = 0; n < newPixmap.getHeight(); n++) {
                    if (n >= 8) {
                        color2 = oldPixmap.getPixel(left + m, bottom + n);
                    } else if (m < 8 && !touchEast) {
                        color2 = currPixmap.getPixel(m, n);
                    } else if (m >= 8 && !touchWest) {
                        color2 = currPixmap.getPixel(m, n);
                    } else if (name.contains("EW")) {
                        color2 = currPixmap.getPixel(m, n);
                    } else {
                        color2 = oldPixmap.getPixel(left + m, bottom + n);
                    }
                    newPixmap.drawPixel(m, n, color2);
                }
            }
            pack(name, newPixmap);
            updateTextureAtlas();
            addToLocalCache(name);
            newPixmap.dispose();
            addedTiles.add(name);
        }
    }

    public static void cacheHouseWithChimney(String name, SmolSprite overSprite) {
        if (!addedTiles.contains(name)) {
            Pixmap oldPixmap = overSprite.getPixmap();
            Pixmap newPixmap = new Pixmap(64, 32, Pixmap.Format.RGBA8888);
            newPixmap.setColor(0.0f, 0.0f, 0.0f, 0.0f);
            SmolSpriteProxy tempSprite = new SmolSpriteProxy(overSprite.getTexture(), overSprite.getRegionX(), overSprite.getRegionY(), overSprite.getRegionWidth(), overSprite.getRegionHeight());
            int left = overSprite.getRegionX();
            int bottom = overSprite.getRegionY();
            TextureData temp = TextureCache.get(Gdx.files.internal("tiles/chimney_sheet1.png")).getTextureData();
            if (!temp.isPrepared()) {
                temp.prepare();
            }
            Pixmap chimneyPixmap = temp.consumePixmap();
            for (int i = 0; i < 4; i++) {
                newPixmap.fill();
                newPixmap.drawPixmap(oldPixmap, 0, 16, left, bottom, 16, 16);
                for (int m = 0; m < newPixmap.getWidth(); m++) {
                    for (int n = 0; n < newPixmap.getHeight(); n++) {
                        int color2 = chimneyPixmap.getPixel(m + (i * 64), n);
                        if (color2 == -9181185) {
                            color2 = Color.rgba8888(tempSprite.color1);
                        } else if (color2 == -14045697) {
                            color2 = Color.rgba8888(tempSprite.color2);
                        }
                        newPixmap.drawPixel(m, n, color2);
                    }
                }
                pack(name, i, newPixmap);
            }
            newPixmap.dispose();
            updateTextureAtlas();
            for (int i2 = 0; i2 < 4; i2++) {
                addToLocalCache(name, i2);
            }
            addedTiles.add(name);
        }
    }

    public static void cacheDarkerTile(String name, SmolSprite sprite) {
        if (!addedTiles.contains(name)) {
            int regionWidth = sprite.getRegionWidth();
            int regionHeight = sprite.getRegionHeight();
            Pixmap atlasPixmap = sprite.getPixmap();
            Pixmap tilePixmap = new Pixmap(regionWidth, regionHeight, atlasPixmap.getFormat());
            tilePixmap.drawPixmap(atlasPixmap, 0, 0, sprite.getRegionX(), sprite.getRegionY(), regionWidth, regionHeight);
            Pixmap newPixmap = new Pixmap(tilePixmap.getWidth(), tilePixmap.getHeight(), Pixmap.Format.RGBA8888);
            int green1_2 = Color.rgba8888(new Color(0.20784314f, 0.40784314f, 0.007843138f, 1.0f));
            int green2_2 = Color.rgba8888(new Color(0.40784314f, 0.80784315f, 0.007843138f, 1.0f));
            int green3_2 = Color.rgba8888(new Color(0.60784316f, 1.0f, 0.20784314f, 1.0f));
            for (int m = 0; m < tilePixmap.getWidth(); m++) {
                for (int n = 0; n < tilePixmap.getHeight(); n++) {
                    int color2 = tilePixmap.getPixel(m, n);
                    if (color2 == 678428927 || color2 == green1_2) {
                        color2 = 760742655;
                    } else if (color2 == 1623722239 || color2 == green2_2) {
                        color2 = 1487864575;
                    } else if (color2 == -1325903617 || color2 == green3_2) {
                        color2 = -2066207233;
                    }
                    newPixmap.drawPixel(m, n, color2);
                }
            }
            pack(name, newPixmap);
            updateTextureAtlas();
            addToLocalCache(name);
            addedTiles.add(name);
        }
    }
}
